package com.sgb.goods.view.activity.vpm;

import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.lifecycle.Observer;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityAddMemberGoodsBinding;
import com.sgb.goods.entity.CommitVipPrice;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.viewmodel.AddMemberGoodsModel;
import com.sgb.goods.widget.DialogHelper;
import com.sgb.goods.widget.pop.SetMemberPricePopWindow;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

/**
 * 新增会员报价
 * fixme: 改天在优化，把对应的几个item做成列表
 * @author cyj
 */
@Route(path = Constants.ARouter.AR_ADD_MEMBER_GOODS)
public class AddMemberGoodsActivity extends BaseActivity<GoodsActivityAddMemberGoodsBinding, AddMemberGoodsModel> {

    private SetMemberPricePopWindow mPopWindow;
    private long itemId;
    private long oldItemMemberId;
    private int code;

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_add_member_goods;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(AddMemberGoodsModel.class, BR.viewModel);
    }

    @Override
    protected void init() {
        mDataBinding.head.title.setText("会员报价");
        itemId = getIntent().getLongExtra("itemId", 0);
        oldItemMemberId = getIntent().getLongExtra("oldItemMemberId", 0);
        mViewModel.getSkuList(itemId);
    }

    @Override
    protected void initObserve() {
        mViewModel.skuList.observe(this, skuListEntities -> {
            if (skuListEntities != null)
                if (code == 50005) {
                    for (int i = 0; i < skuListEntities.size(); i++) {
                        //比对促销价格或者销售价格是否不一样
                        if (!skuListEntities.get(i).promotePrice.equals(mViewModel.adapter.getData().get(i).promotePrice)
                                || !skuListEntities.get(i).money.equals(mViewModel.adapter.getData().get(i).money)) {
                            mViewModel.adapter.getData().get(i).money = skuListEntities.get(i).money;
                            mViewModel.adapter.getData().get(i).promotePrice = skuListEntities.get(i).promotePrice;
                            mViewModel.adapter.getData().get(i).isSet = false;
                            mViewModel.adapter.getData().get(i).isChange = true;
                        }
                    }
                    mViewModel.adapter.notifyDataSetChanged();
                } else {
                    mViewModel.adapter.setNewData(skuListEntities);
                }
        });
        mViewModel.status.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                //50002 企业注销
                //50003 下架删除
                //50004 sku变化
                //50001 有会员价格异常code码
                if (integer == 200) {
                    finish();
                } else if (integer == 50002) {
                    DialogHelper.showDialog(AddMemberGoodsActivity.this,
                            "温馨提示",
                            "操作失败！企业已注销，店铺关闭",
                            "",
                            "知道了", null);
                } else if (integer == 50003) {
                    DialogHelper.showDialog(AddMemberGoodsActivity.this,
                            "温馨提示",
                            "操作失败！当前商品已下架/不存在",
                            "",
                            "知道了",
                            new DialogHelper.DialogFunction() {
                                @Override
                                public void normalRoundDialog() {
                                    finish();
                                }
                            });
                } else if (integer == 50004) {
                    DialogHelper.showDialog(AddMemberGoodsActivity.this,
                            "温馨提示",
                            "操作失败！当前商品发生变化",
                            "",
                            "知道了",
                            new DialogHelper.DialogFunction() {
                                @Override
                                public void normalRoundDialog() {
                                    finish();
                                }
                            });
                } else if (integer == 50005) {
                    code = 50005;
                    DialogHelper.showDialog(AddMemberGoodsActivity.this,
                            "温馨提示",
                            "操作失败！当前商品价格发生变化",
                            "",
                            "知道了",
                            new DialogHelper.DialogFunction() {
                                @Override
                                public void normalRoundDialog() {
                                    mViewModel.getSkuList(itemId);
                                }
                            });
                } else if (integer == 50001) {
                    code = 50001;
                    DialogHelper.showDialog(AddMemberGoodsActivity.this,
                            "温馨提示",
                            "操作失败！当前商品已在其他端设置成功",
                            "",
                            "知道了",
                            new DialogHelper.DialogFunction() {
                                @Override
                                public void normalRoundDialog() {
                                    finish();
                                }
                            });
                }
            }
        });
    }

    @Override
    protected void initListener() {
        mDataBinding.head.ivBack.setOnClickListener(v -> {
            DialogHelper.showDialog(AddMemberGoodsActivity.this,
                    "温馨提示",
                    "此操作将丢失当前表单填写的内容，您还要继续吗？",
                    "取消",
                    "确定",
                    new DialogHelper.DialogFunction() {
                        @Override
                        public void normalRoundDialog() {
                            finish();
                        }
                    });
        });
        mDataBinding.btCommit.setOnClickListener(v -> checkData());
        mViewModel.adapter.setOnItemClickListener((adapter, view, position) -> {
            if (mPopWindow == null) {
                mPopWindow = new SetMemberPricePopWindow(AddMemberGoodsActivity.this);
                mPopWindow.setBgView(mDataBinding.grayLayout);
                mPopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
                mPopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
            }
            mPopWindow.setOnResultClick((key1, key2, key3) -> {
                code = 0;
                mViewModel.adapter.notifyDataSetChanged();
            });
            mPopWindow.setData(mViewModel.adapter.getItem(position));
            mPopWindow.showDownPopwindow(mDataBinding.grayLayout, true);
        });
    }

    private void checkData() {
        CommitVipPrice commitVipPrice = new CommitVipPrice();
        List<CommitVipPrice.ItemSkuMemberPriceDTOS> list = new ArrayList<>();
        commitVipPrice.itemId = itemId + "";
        commitVipPrice.oldItemMemberId = oldItemMemberId + "";
        for (int i = 0; i < mViewModel.adapter.getData().size(); i++) {
            if (!mViewModel.adapter.getData().get(i).isSet) {
                MToast.showToast(AddMemberGoodsActivity.this, "请填写会员价");
                return;
            }
            for (int j = 0; j < 6; j++) {
                CommitVipPrice.ItemSkuMemberPriceDTOS skuMemberPriceDTOS = new CommitVipPrice.ItemSkuMemberPriceDTOS();
                skuMemberPriceDTOS.itemId = itemId + "";
                skuMemberPriceDTOS.level = j;
                skuMemberPriceDTOS.parityPrice = mViewModel.adapter.getData().get(i).money;
                skuMemberPriceDTOS.promotionPrice = mViewModel.adapter.getData().get(i).promotePrice;
                skuMemberPriceDTOS.skuDetails = mViewModel.adapter.getData().get(i).attr;
                skuMemberPriceDTOS.skuId = mViewModel.adapter.getData().get(i).id;
                switch (j) {
                    case 0:
                        skuMemberPriceDTOS.price = mViewModel.adapter.getData().get(i).price;
                        break;
                    case 1:
                        skuMemberPriceDTOS.price = mViewModel.adapter.getData().get(i).price1;
                        break;
                    case 2:
                        skuMemberPriceDTOS.price = mViewModel.adapter.getData().get(i).price2;
                        break;
                    case 3:
                        skuMemberPriceDTOS.price = mViewModel.adapter.getData().get(i).price3;
                        break;
                    case 4:
                        skuMemberPriceDTOS.price = mViewModel.adapter.getData().get(i).price4;
                        break;
                    case 5:
                        skuMemberPriceDTOS.price = mViewModel.adapter.getData().get(i).price5;
                        break;
                }
                list.add(skuMemberPriceDTOS);
            }
        }
        commitVipPrice.itemSkuMemberPriceDTOS = list;
        mViewModel.commitData.setValue(commitVipPrice);
        mViewModel.commitVipPrice();
    }
}
